package com.mimose.cosy.codec;

import com.mimose.cosy.excepitons.CommonRuntimeException;
import com.mimose.cosy.lang.Strings;

/**
 * @author mimose
 * @description base64
 * @date 2021/4/24
 */
public final class Base64 {
    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    private static final char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();

    private static final byte[] CODES = new byte[256];

    static  {
        for (int i = 0; i < 256; i++) {
            CODES[i] = -1;
        }
        for (int i = 65; i <= 90; i++) {
            CODES[i] = (byte)(i - 65);
        }
        for (int i = 97; i <= 122; i++) {
            CODES[i] = (byte)(26 + i - 97);
        }
        for (int i = 48; i <= 57; i++) {
            CODES[i] = (byte)(52 + i - 48);
        }
        CODES[43] = 62;
        CODES[47] = 63;
    }

    public static String encodeToString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(byteToHex(b));
        }
        return sb.toString();
    }

    public static String encodeToString(String text) {
        if (Strings.isEmpty(text)) {
            return text;
        }

        return encodeToString(text.getBytes());
    }

    public static char[] encode(byte[] data) {
        char[] out = new char[(data.length + 2) / 3 * 4];

        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
            boolean quad = false;
            boolean trip = false;
            int val = 0xFF & data[i];
            val <<= 8;
            if (i + 1 < data.length) {
                val |= 0xFF & data[i + 1];
                trip = true;
            }
            val <<= 8;
            if (i + 2 < data.length) {
                val |= 0xFF & data[i + 2];
                quad = true;
            }
            out[index + 3] = ALPHABET[quad ? (val & 0x3F) : 64];
            val >>= 6;
            out[index + 2] = ALPHABET[trip ? (val & 0x3F) : 64];
            val >>= 6;
            out[index + 1] = ALPHABET[val & 0x3F];
            val >>= 6;
            out[index] = ALPHABET[val & 0x3F];
        }

        return out;
    }

    public static byte[] decode(byte[] bytes) {
        char[] chars = CodecSupport.toChars(bytes);
        return decode(chars);
    }

    public static byte[] decode(char[] data) {
        if (data == null) {
            return null;
        }

        int len = (data.length + 3) / 4 * 3;
        if (data.length > 0 && data[data.length - 1] == '=') {
            len--;
        }
        if (data.length > 1 && data[data.length - 2] == '=') {
            len--;
        }
        byte[] out = new byte[len];
        int shift = 0;
        int accum = 0;
        int index = 0;
        for (char aData : data) {
            int value = CODES[aData & 0xFF];
            if (value >= 0) {
                accum <<= 6;
                shift += 6;
                accum |= value;
                if (shift >= 8) {
                    shift -= 8;
                    out[index++] = (byte)(accum >> shift & 0xFF);
                }
            }
        }
        if (index != out.length) {
            throw new CommonRuntimeException("miscalculated data length!");
        }
        return out;
    }

    public static String decodeToString(String text) {
        if (Strings.isEmpty(text)) {
            return text;
        }
        return new String(decode(text.toCharArray()));
    }

    public static String decodeToString(byte[] bytes) {
        String text = CodecSupport.toString(bytes);
        return decodeToString(text);
    }

    public static String encode(String text) {
        if (Strings.isEmpty(text)) {
            return text;
        }
        byte[] bytes = CodecSupport.toBytes(text);
        char[] chars = encode(bytes);
        return CodecSupport.toString(chars);
    }

    private static String byteToHex(byte b) {
        short s = b;
        if (s < 0) {
            s += 256;
        }
        int d1 = s / 16;
        int d2 = s % 16;

        String charOne = String.valueOf(HEX_ARRAY[d1]);
        String charTwo = String.valueOf(HEX_ARRAY[d2]);
        return charOne + charTwo;
    }
}
